<?php

namespace App\Service\Indent;

use App\Models\Indent\OrderGoodsModel;
use App\Models\Indent\OrderModel;
use DB;

class OrderService
{
    public static function generateOrderId($uid = "", $type = "pt")
    {
        return $type . date('ymdhis', time()) . substr($uid, 12, 2);
    }

    /**
     * 生成订单
     * @param array $info
     * @return bool
     */
    public static function createOrder(array $info)
    {
        /**
         * 插入订单
         */
        if ($order = OrderModel::create($info)) {
            return $order;
        }else{
            return false;
        }
    }

    /**
     * 获得订单
     */
    public static function getOrder($order_id)
    {
        $order_info = OrderModel::where('id', $order_id)
            ->first();
        return $order_info;
    }

    /**
     * 更新订单
     */
    public static function changeOrder($order_id, array $data)
    {
        $result = DB::table('lsj_order')->where('order_id', $order_id)->update($data);
        return $result;
    }


    /**
     * 获得订单
     */
    public static function getOrderArray(array $array)
    {
        $order_info = OrderModel::where($array)
            ->first();

        return $order_info;
    }

    /**
     * 获取我的购买
     */
    public static function getMyBuys($uid = '', $page, $offset)
    {
        $result = DB::select("select
(select count(*) from lsj_praise_log b where b.articleid=a.id)  as praise_num,
(select count(*) from lsj_comment_log b where a.id=b.worksid) as comment_num,
(select count(*) from lsj_order b where a.id=b.article_id and b.paystatus=1) as order_num,
(select count(*) from lsj_praise_log b where b.articleid=a.id and b.uid= ?) as is_praise,
a.id,a.title,a.introduction,a.price,a.thumb,a.create_time,a.author_uid,a.reading,a.article_class,b.nickname as author,b.headimgurl,a.remark from 
(select article_id,id as order_id from lsj_order 
where uid=? and order_type=1 and paystatus=1
order by id desc 
limit ?,?)c,lsj_article a 
left join lsj_user b on a.author_uid=b.uid
where c.article_id=a.id
order by c.order_id", [$uid, $uid, $offset, $page]);

        return $result;
    }

    /**
     * 获得我收益的总额
     */
    public static function getMyIncome($uid)
    {
        $incomes = DB::table('lsj_user')
            ->where('uid', $uid)
            ->select('balance')
            ->first();
        return $incomes->balance;
    }

    /**
     * 增加订单明细
     */
    public static function addOrderDetail(array $data)
    {
        $result = DB::table('lsj_order_detail')
            ->insert($data);
        return $result;
    }

    /**
     * 增加介入明细
     */
    public static function addPlatIn(array $data)
    {
        $result = DB::table('lsj_plat_in')
            ->insert($data);

        return $result;
    }


    /**
     * 订单查询
     */
    public static function selectMyOrder($order_status, $uid)
    {
        $where = [
            'uid' => $uid
        ];

        if($order_status != ''){
            $where['status'] = $order_status;
        }

        $order_info = OrderModel::where($where)->with('inquirySheet','orderShipAddress')->Paginate(20);

        return $order_info;
    }

    /**
     * 订单详情
     */
    public static function getOrderDetail($order_id = '',$uid)
    {
        $result = DB::table('lsj_order as a')
            ->leftJoin('lsj_inquiry_sheet as b', 'a.inquiry_sheet_id', '=', 'b.id')
            ->leftJoin('lsj_user as c', 'a.uid', '=', 'c.uid')
            ->leftJoin('lsj_order_ship_address as d', 'a.order_id','=','d.order_id')
            ->leftJoin('lsj_order_courier as e','a.order_id','=','e.order_id')
            ->where('a.id', $order_id)
            ->where('a.uid',$uid)
            ->select('a.order_id', 'a.price', 'a.status', 'a.remark', 'a.price','a.created_at','a.id',
                'b.goods_name', 'b.goods_num','b.goods_id','b.goods_thumb','b.quote_price', 'b.goods_spec',
                'c.nickname', 'c.headimgurl','d.name','d.phone','d.province','d.city','d.area','d.address','e.courier_company','e.courier_code');

        $result = $result->first();

        return $result;
    }

    /**
     * 查询余额 加排他锁
     */
    public static function selectUserBalanceLock($uid = '')
    {
        $incomes = DB::table('lsj_user')
            ->where('uid', $uid)
            ->select('balance')
            ->lockForUpdate()
            ->first();
        return $incomes->balance;
    }

    /**
     * 增加订单地址
     * @param $address_id
     * @return mixed
     */
    public static function addOrderAddress($address,$order_id,$uid)
    {
        $result =DB::table('lsj_order_ship_address')->insert([
            'order_id' => $order_id,
            'address' => $address->address,
            'name' => $address->name,
            'phone' => $address->phone,
            'province_id' => $address->province_id,
            'province' => $address->province,
            'city_id' => $address->city_id,
            'city' => $address->city,
            'area_id' => $address->area_id,
            'area' => $address->area,
            'uid' => $uid,
            'code' => $address->code
        ]);
        return $result;
    }

    /**
     * 订单商品
     */
    public static function createOrderGoods($order_id,$data,$number)
    {
        $order_goods = array(
            'order_id' => $order_id,
            'goods_id' => $data['id'],
            'name' => $data['name'],
            'orgin_price' => $data['orgin_price'],
            'current_price' => $data['current_price'],
            'thumb' => $data['thumb'],
            'stock' => $data['stock'],
            'spec' => $data['spec'],
            'description' => $data['description'],
            'number' => $number
        );

        return DB::table('lsj_order_goods')->insert($order_goods);
    }

    /**
     * 购物车商品
     */
    public static function createCartOrderGoods($data)
    {
        return OrderGoodsModel::insert($data);
    }

    /**
     * 获得订单商品
     */
    public static function getOrderGoods($order_id)
    {
        return OrderGoodsModel::where('order_id',$order_id)->get();
    }

    /**
     * 根据order_id 和 goods_id 获得订单商品
     */
    public static function getOrderGoodsByArray($where = [])
    {
        return OrderGoodsModel::where($where)->get();
    }

    /**
     * 更新商品订单状态
     */
    public static function setOrderGoodsStatus($order_id,$goods_id,$status)
    {
        return OrderGoodsModel::where([
            'order_id' => $order_id,
            'goods_id' => $goods_id
        ])->update([
            'status' => $status
        ]);
    }
}